@echo off

set BUILD_PATH=%CD%
set QMAKE_CACHE=%BUILD_PATH%\.qmake.cache
set PROJECT_LOG=%BUILD_PATH%\config.log

if exist "%QMAKE_CACHE%" del %QMAKE_CACHE%
if exist "%PROJECT_LOG%" del %PROJECT_LOG%

:cmdline_parsing
if "%1" == ""               goto startProcessing
if "%1" == "-sourcepath"    goto setSourcePath
echo Unknown option: "%1"
goto usage

:setSourcePath
shift
set SOURCE_PATH=%1%
shift
echo.
echo Checking Mobile Extensions sources...
if not exist "%SOURCE_PATH%\src.pro" goto errorSources
echo Found.
echo MOBILE_EXTENSIONS_PRFPATH = %BUILD_PATH%\features | perl -pe "s/\\/\//g" >> %QMAKE_CACHE%
echo MOBILE_EXTENSIONS_SOURCEPATH = %SOURCE_PATH% | perl -pe "s/\\/\//g" >> %QMAKE_CACHE%
goto cmdline_parsing

:startProcessing
if "%SOURCE_PATH%" == ""   goto usage
echo.
echo Checking Qt version
call %QT_PATH%qmake -v >> %PROJECT_LOG% 2>&1
if errorlevel 1 goto qmakeNotFound
goto qmakeFound
:qmakeNotFound
echo ... Not found  >> %PROJECT_LOG% 2>&1
if "%QT_PATH%" == "" (
    echo >&2Cannot find 'qmake' in your PATH.
    echo >&2Your PATH is: %PATH%
) else (
    echo >&2Cannot find 'qmake' in %QT_PATH%.
)
echo >&2Aborting.
goto errorExit

:qmakeFound
call %QT_PATH%qmake -query QT_VERSION
echo.
echo Running qmake...
call %QT_PATH%qmake %BUILD_PATH%/mobile_extensions.pro
if errorlevel 1 goto qmakeRecError
echo.
echo configure finished. Configuration feature 'mobile_extensions' successfully integrated to Qt.
goto cleanExit

:qmakeRecError
echo.
echo configure failed.
goto cleanExit

:usage
echo Usage: configure.bat -sourcepath (dir)
    echo.
    echo Options:
    echo.
    echo -sourcepath (dir) ...... Specify location of source directory of 
    echo                          Mobile Extensions for Qt for Symbian
    echo.
    echo Example: configure.bat -sourcepath C:\Qt\MobileExtensions\extensions\src

goto errorExit

:errorSources
echo.
echo Configure failed: Cannot locate source files from %SOURCE_PATH%
echo.

:errorExit
set QMAKE_CACHE=
set SOURCE_PATH=
set BUILD_PATH=
exit /b 1

:cleanExit
set QMAKE_CACHE=
set SOURCE_PATH=
set BUILD_PATH=
exit /b 0
